<script>
	import {
		useAppStore,
		useUserStore
	} from "@/store/index.js"
	import {
		storeToRefs
	} from "pinia"
	import {
		MAP_KEY
	} from "@/config/app.js"

	export default {
		onLaunch: function() {
			const {
				location
			} = storeToRefs(useAppStore())
			// 首次打开初始化地址
			uni.getLocation({
				success(res) {
					let lat = res.latitude;
					let lng = res.longitude;
					uni.request({
						url: `https://restapi.amap.com/v3/geocode/regeo?key=${MAP_KEY}&location=${lng},${lat}`,
						method: "GET",
						success(amap_res) {
							
							location.value.lat = lat
							location.value.lng = lng
							location.value.city = amap_res.data.regeocode.addressComponent.district
							location.value.province = amap_res.data.regeocode.addressComponent.province
							uni.setStorageSync("location",location.value)
							// 触发全局事件
							    uni.$emit('locationUpdated', location.value)
						},
						fail() {
							uni.showToast({
								'title': '对不起，数据获取失败！',
								'icon': 'none'
							})
						},

					})
				},
				fail(err) {
					// #ifdef MP
					uni.showModal({
						title: '提示',
						content: '未开通定位权限将无法使用完整功能，是否去开启定位权限',
						showCancel: false,
						confirmText: "开通权限",
						success: res => {
							if (res.confirm) {
								// 跳转设置页面
								uni.openSetting({
									success: res => {

									}
								});
							}
						}
					});
					// #endif
				}
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	@import "@/static/icons/iconfont.css";
	@import "@/static/icons/iconfont_color/iconfont_color.css";
	@import "@/uni_modules/uview-plus/index.scss";
	@import "@/static/css/common.scss";
	@import "@/static/icons/SemiBold/SemiBold.css";
</style>